﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using Shrestha.Data.Commands;
using Shrestha.Data.WizardPages;
using System.Windows.Navigation;
using Shrestha.Data.Database;
using System.Windows;

namespace Shrestha.Data
{
    /// <summary>
    /// This class is the view model class of the main importer window.
    /// </summary>
    public class DataWizardViewModel
    {
        private readonly DataWizard _dw;

        public ICommand NextPageCommand { get; set; }

        public String SelectedDataSource { get; set; }
        public string SelectedTable { get; set; }
        
        public String DbHost { get; set; }
        public String DbUser { get; set; }
        public String DbName { get; set; }
        public String DbPassword { get; set; }

        public Frame NavFrame { get; set; }

        public Object ConnectionObject { get; set; }

        private IDbManager _dbman;

        private enum AvailablePages { SOURCE, DB, COLUMNS, TABLE, END }
        private AvailablePages _nextpage;

        public List<String> TableNames { get; set; }
        public List<String> ColumnNames { get; set; }

        public DataWizardViewModel(DataWizard dataWizard, IDbManager dbman)
        {
            NextPageCommand = new NextPageCommand(this);

            NavFrame = new Frame()
            {
                NavigationUIVisibility = NavigationUIVisibility.Visible,
                TabIndex = 0
            };

            this._dw = dataWizard;
            this._dbman = dbman;
            this._nextpage = AvailablePages.SOURCE;

            DbHost = "192.168.0.11"; DbName = "eurovis"; DbUser = "ayush";

            NavFrame.NavigationService.Navigating += new NavigatingCancelEventHandler(NavigationService_Navigating);

            SetPage();

        }

        void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            if (e.NavigationMode == NavigationMode.Back)
            {
                switch (_nextpage)
                {
                    case AvailablePages.DB:
                        this._nextpage = AvailablePages.SOURCE;
                        break;
                    case AvailablePages.TABLE:
                        this._nextpage = AvailablePages.DB;
                        break;
                    case AvailablePages.COLUMNS:
                        this._nextpage = AvailablePages.TABLE;
                        break;
                    case AvailablePages.END:
                        this._nextpage = AvailablePages.COLUMNS;
                        break;
                }
            }
        }

        /// <summary>
        /// This function sets the page in the DataWizard. 
        /// </summary>
        public void SetPage()
        {
            switch (_nextpage)
            {
                case AvailablePages.SOURCE:
                    NavFrame.NavigationService.Navigate(new SelectDataSource(this));
                    this._nextpage = AvailablePages.DB;
                    break;
                case AvailablePages.DB:
                    NavFrame.NavigationService.Navigate(new SelectDb(this));
                    this._nextpage = AvailablePages.TABLE;
                    break;
                case AvailablePages.TABLE:
                    //_dbman = new MySqlManager(this);
                    ConnectionObject = _dbman.GetConnection(DbHost, DbName, DbUser, DbPassword);
                    this.TableNames = _dbman.GetTableNames(ConnectionObject);
                    NavFrame.NavigationService.Navigate(new SelectTable(this));
                    this._nextpage = AvailablePages.COLUMNS;
                    break;
                case AvailablePages.COLUMNS:
                    //if (_dbman == null) _dbman = new MySqlManager(this);
                    this.ColumnNames = _dbman.GetColumnNames(ConnectionObject, SelectedTable);
                    NavFrame.NavigationService.Navigate(new SelectColumns(this));
                    this._nextpage = AvailablePages.END;
                    break;
                case AvailablePages.END:
                    _dw.ImportResult = true;
                    _dw.Close();
                    break;
            }
        }

        
    }
}
